package db.swing2;

import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultTreeModel;

import db.swing2.Controller.MyHitListSelectionListener;
import db.swing2.Controller.MyWindowClosingListener;
import db.swing2.Controller.SearchNameActionListener;
import de.dnb.basics.Misc;
import de.dnb.basics.applicationComponents.RangeCheckUtils;
import de.dnb.gnd.parser.Format;
import de.dnb.gnd.parser.Record;
import de.dnb.gnd.utils.GNDUtils;
import de.dnb.gnd.utils.RecordUtils;
import de.dnb.gnd.utils.formatter.HTMLFormatter;

public final class View {

	public View() {
		super();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					gui = new GUI();
					gui.setVisible(true);
					gui.splitPaneHitRecord.setDividerLocation(.55f);
					gui.splitPaneHitTree.setDividerLocation(.3f);
					gui.splitPaneOBUB.setDividerLocation(.5);
					gui.treeOBB.setCellRenderer(new HideTreeRootRenderer());
					gui.treeUBB.setCellRenderer(new HideTreeRootRenderer());
					setBusyCursor(true);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private GUI gui;

	private HTMLFormatter formatter = new HTMLFormatter();

	public String getSelectedRadioButton() {
		Enumeration<AbstractButton> enumeration =
				gui.buttonGroupType.getElements();
		while (enumeration.hasMoreElements()) {
			AbstractButton button = enumeration.nextElement();
			if (button.isSelected())
				return button.getText();
		}
		return null;
	}

	public String getSearchTerm() {
		return gui.textFieldSearchTerm.getText();
	}

	public String getSearchIDN() {
		return gui.textFieldIDN.getText();
	}

	public int getSelectedHitListRow() {
		return gui.tableHitList.getSelectedRow();
	}

	public void setRecord(Record record) {
		if (record == null) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					gui.editorPaneRecord
							.setText("Datensatz konnte nicht geladen werden");
				}
			});
			return;
		}
		RangeCheckUtils.assertReferenceParamNotNull("record", record);
		String name = GNDUtils.getNameOfRecord(record);
		gui.labelRecordName.setText(name);

		String recordText = formatter.format(record);
		final String text =
				"<body bgcolor=\"#f6f6f6\">" + "<h2>IDN: " + record.getId()
						+ "</h2><nobr>" + recordText + "</nobr>" + "</body>";

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				gui.editorPaneRecord.setText(text);
			}
		});
		// cursor auf Anfang setzen:
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				gui.editorPaneRecord.setCaretPosition(0);
			}
		});

	}

	public void setLinkListener(HyperlinkListener listener) {
		gui.editorPaneRecord.addHyperlinkListener(listener);
	}

	public void setStatus(final String text) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.labelStatus.setText(text);
			}
		});

	}

	public void setSearchTerm(final String text) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.labelSearchTerm.setText(text);
			}
		});

	}

	public final void setTreeSelectionListener(
			final TreeSelectionListener listener) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.treeUBB.addTreeSelectionListener(listener);
				gui.treeOBB.addTreeSelectionListener(listener);
			}
		});
	}

	public final void setOBBTreeMouseListener(MouseListener listener) {
		gui.treeOBB.addMouseListener(listener);
	}

	public final void setUBBTreeMouseListener(MouseListener listener) {
		gui.treeUBB.addMouseListener(listener);
	}

	public void setUBTree(final OBUBTreeNode rootNode) {
		if (rootNode != null) {
			int childrenCount = rootNode.getChildCount();
			gui.lblUBCount.setText(Integer.toString(childrenCount));
		} else
			gui.lblUBCount.setText("");
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.labelRootName.setText(rootNode.toString());
				gui.treeUBB.setModel((new DefaultTreeModel(rootNode)));
				gui.scrollPaneUB.getVerticalScrollBar().getModel().setValue(0);
				gui.scrollPaneUB.getHorizontalScrollBar().getModel()
						.setValue(0);
			}
		});
	}

	public void setOBTree(final OBUBTreeNode rootNode) {
		if (rootNode != null) {
			int childrenCount = rootNode.getChildCount();
			gui.lbOBCount.setText(Integer.toString(childrenCount));
		} else
			gui.lbOBCount.setText("");
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.treeOBB.setModel((new DefaultTreeModel(rootNode)));
			}
		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.scrollPaneOB.getVerticalScrollBar().getModel().setValue(0);
				gui.scrollPaneOB.getHorizontalScrollBar().getModel()
						.setValue(0);
			}
		});
	}

	public void setSearchIDActionListener(ActionListener listener) {
		gui.btnIdnSearchID.addActionListener(listener);
		gui.textFieldIDN.addActionListener(listener);
	}

	public void setSearchNameActionListener(SearchNameActionListener listener) {
		gui.btnSearchName.addActionListener(listener);
		gui.textFieldSearchTerm.addActionListener(listener);
		Enumeration<AbstractButton> enumeration =
				gui.buttonGroupType.getElements();
		while (enumeration.hasMoreElements()) {
			AbstractButton button = enumeration.nextElement();
			button.addActionListener(listener);
		}

	}

	public void close() {
		gui.setVisible(false);
		gui.dispose();
	}

	public void addWindowClosingListener(
			MyWindowClosingListener myWindowListener) {
		gui.addWindowListener(myWindowListener);
	}

	public void
			setHitListSelectionListener(MyHitListSelectionListener listener) {
		gui.tableHitList.getSelectionModel().addListSelectionListener(listener);

	}

	public void setHitListModel(final HitListTableModel model) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if (model == null)
					setHitCount(0);
				else
					setHitCount(model.getRowCount());
				gui.tableHitList.setModel(model);
				TableColumnModel colMod = gui.tableHitList.getColumnModel();
				colMod.getColumn(0).setMaxWidth(5);
				colMod.getColumn(1).setMaxWidth(5);
				colMod.getColumn(3).setMaxWidth(70);
				gui.scrollPaneHilList.getVerticalScrollBar().getModel()
						.setValue(0);
			}
		});
	}

	public void setHitCount(final int hitCount) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.lblHitCount.setText(Integer.toString(hitCount));
			}
		});

	}

	public JTree getOBBTree() {
		return gui.treeOBB;
	}

	public JTree getUBBTree() {
		return gui.treeUBB;
	}

	public void setOBExpandListener(ActionListener listener) {
		gui.btnOBExpanded.addActionListener(listener);
	}

	public void setUBExpandListener(ActionListener listener) {
		gui.btnUBBExpanded.addActionListener(listener);
	}

	public HitListTableModel getHitListTableModel() {
		return (HitListTableModel) gui.tableHitList.getModel();
	}

	public void addError(final String error) {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				String errors = gui.textPaneErrors.getText();
				errors += "\n" + error;
				gui.textPaneErrors.setText(errors);
			}
		});
	}

	public void setBusyCursor(boolean busy) {

		if (busy)
			gui.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		else
			gui.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}
}
